var http = require( '../../utils/util' )
import { restUserAddress, restUserAddressSave } from '../../api/user.js'
var app = getApp()

Page({ 
  data: {
    items: [
      {name: '是', value: 1, checked: 'true'},
      {name: '否', value: 0},
    ],
	addressBean: "",
	editinfo:""
  },
  
  onLoad: function(e){
	  var that = this;
	if(e.params){
		var params = JSON.parse(e.params)
		console.log(params);
		that.setData({
			editinfo: params, 
			id: params.id, 
			latitude: params.latitude , 
			longitude: params.longitude ,
			REGION_PROVINCE: params.province ,
			REGION_CITY: params.city ,
			REGION_COUNTRY: params.district ,
			ADDRESS: params.province+params.city+params.district,
		});
	}
  },
  
  formSubmit: function(e) {
	var that = this;
	var data = {
		city: that.data.REGION_CITY,
		district: that.data.REGION_COUNTRY,
		province: that.data.REGION_PROVINCE,
		latitude: that.data.latitude,
		longitude: that.data.longitude,
	};
	
	if(!e.detail.value.address){
		wx.showToast({
			title: "请填写详细地址",
			icon: "error"
		});
	}
	
	if(! /^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(e.detail.value.mobile)){
		wx.showToast({
			title: "请填写真实手机",
			icon: "error"
		});
	}
	
	if(!e.detail.value.name){
		wx.showToast({
			title: "请填写收货姓名",
			icon: "error"
		});
	}
	
	data.address = e.detail.value.address;
	data.baseaddress = that.data.ADDRESS+e.detail.value.address;
	data.mobile = e.detail.value.mobile;
	data.userName = e.detail.value.name;
	data.isDefault = e.detail.value.isDefault;
	data.userId = app.globalData.wxlogin.userId;
	
	if(that.data.id){
		data.id = that.data.id;
	}
	
	restUserAddressSave(data).then(res => {
		if(res.code == 0){
			var title = "新增成功!";
			if( that.data.id ){
				title = "修改成功!";
			}
			
			wx.showToast({title: title, icon: "success"});
			setTimeout(function(){
				wx.navigateBack();
			}, 1555);
		}else{
			wx.showToast({title: res.msg, icon: "fail"});
		}
	});
	
  },
  
  
  choosePoi: function() {
	  var that = this;
	  // wx.choosePoi({
		 //  success: function(res){
			//   console.log(res);
			//   _that.setDate({ addressPoi: res.address });
		 //  },
		 //  fail: function(){
			//   wx.showToast({title: "未开启地理位置!"});
		 //  }
	  // });
	  wx.choosePoi({
		  success: function(res){
			  console.log(res);
			  var regex = /^(北京市|天津市|重庆市|上海市|香港特别行政区|澳门特别行政区)/;  
			  var REGION_PROVINCE=[];  
			  var addressBean = {  
				REGION_PROVINCE:null,  
				REGION_COUNTRY:null,  
				REGION_CITY:null,
				ADDRESS:null,
				latitude: res.latitude,
				longitude: res.longitude};
			  function regexAddressBean(address, addressBean){  
				  regex = /^(.*?[市州]|.*?地区|.*?特别行政区)(.*?[市区县])(.*?)$/g;  
				  var addxress = regex.exec(address);  
				  addressBean.REGION_CITY=addxress[1];  
				  addressBean.REGION_COUNTRY=addxress[2];  
				  addressBean.ADDRESS=addxress[3]+"("+res.name+")";  
				  //console.log(addxress);  
			  }
			  if(!(REGION_PROVINCE = regex.exec(res.address))){  
				regex = /^(.*?(省|自治区))(.*?)$/;  
				REGION_PROVINCE = regex.exec(res.address);  
				addressBean.REGION_PROVINCE= REGION_PROVINCE[1];
				console.log(regex.exec(res.address));
				regexAddressBean(REGION_PROVINCE[3],addressBean);  
			  } else {
				addressBean.REGION_PROVINCE= REGION_PROVINCE[1];  
				regexAddressBean(res.address, addressBean);  
			  }
			  addressBean.ADDRESS=addressBean.REGION_PROVINCE+addressBean.REGION_CITY+addressBean.REGION_COUNTRY+addressBean.ADDRESS;  
			  that.setData(addressBean);
			  //ADDRESS: "应嘉路"
				// REGION_CITY: "宁波市"
				// REGION_COUNTRY: "江北区"
				// REGION_PROVINCE: "浙江省"
		  },
		  fail: function(){
			  //wx.showToast({title: "未开启地理位置!"});
		  }
	  });
  },
  
  radioChange: function(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
  }
})